﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Drawing;
using System.Drawing.Imaging;

namespace ChartControls
{
    [DefaultProperty("Text")]
    [ToolboxData("<{0}:ServerControl1 runat=server></{0}:ServerControl1>")]
    public class XYChart : CFreeChart
    {

        private XYDataSet xYDataSet;

        public XYDataSet XYDataSet
        {
            get { return xYDataSet; }
            set { xYDataSet = value; }
        }

        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string Text
        {
            get
            {
                String s = (String)ViewState["Text"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["Text"] = value;
            }
        }


        [Bindable(true)]
        [Category("Appearance")]
        [DefaultValue("")]
        [Localizable(true)]
        public string aaaa 
        {
            get
            {
                String s = (String)ViewState["dddd"];
                return ((s == null) ? "[" + this.ID + "]" : s);
            }

            set
            {
                ViewState["dddd"] = value;
            }
        }

        protected override void RenderContents(HtmlTextWriter output)
        {
            output.Write("<img src='getimage.aspx'/>");
        }

        public Bitmap CreateChart()
        {
            int width = 300;
            int height = 300;

            Bitmap bitMap = new Bitmap(width, height);
            Graphics g = Graphics.FromImage(bitMap);

            Font font = new Font("Consolas", 20);
            Brush solidBrush = new SolidBrush(Color.Lime);
            //Pen pen = new Pen(Color.Red, 5);
            Pen pen = new Pen(solidBrush);

            Point xyStartPoint = new Point(width / 6, (height / 6) * 5);
            Point yEndPoint = new Point(width / 6, (height / 6));

            Point xEndPoint = new Point(width / 6 * 5, xyStartPoint.Y);

            //draw x line
            g.DrawLine(pen, xyStartPoint, xEndPoint);

            //draw y line
            g.DrawLine(pen, xyStartPoint, yEndPoint);


            g.Dispose();

            return bitMap;
        } 

        public override void SaveAsJPEG(FileStream fs)
        {
            this.CreateChart().Save(fs, ImageFormat.Jpeg);
        }
    }
}
